<template>
	<view class="container">
		<uni-list v-if="showStatus">
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="猪只耳号"
				:rightText="pigLiveList.pigCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="已产胎次"
						   :rightText="pigLiveList.pigLivedNum + '次'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="栏舍类型"
				:rightText="phouseType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="栏舍编码"
				:rightText="pigLiveList.phouseCode" />

			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="公猪耳号"
						   :rightText="pigLiveList.pigFatherCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="妊娠天数"
						   :rightText="pigLiveList.pregnancyDays+'天'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'calendar-filled'}" title="分娩时间"
						   :rightText="pigLiveList.pigLiveDatetime" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="产程时间"
						   :rightText="pigLiveList.pigLiveTime + 'h'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="健仔数量"
						   :rightText="pigLiveList.pigHealthyNum+'个'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="弱仔数量"
						   :rightText="pigLiveList.pigWeakNum +'个'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="畸形数量"
						   :rightText="pigLiveList.pigMalfmNum +'个'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="死胎数量"
						   :rightText="pigLiveList.pigDeadNum +'个'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="母活仔数"
						   :rightText="pigLiveList.pigLiveFemaleAliveNum+'个'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="分娩方式"
						   :rightText="pigLiveType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="分娩负责人"
				:rightText="pigLiveList.hmyStaff.staffName" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'chat-filled'}" title="备注">
				<template v-slot:footer>
					<input type="textarea" v-model="pigLiveList.remark"
						style="font-size: 12px;color: #999999;text-align: right;" />
				</template>
			</uni-list-item>

		</uni-list>
		<view class="button-group" v-if="showStatus">
			<button type="warn" plain="true" @click="pigLiveDelete" style="width: 45%;border: #E53F32 solid 1px;">删 除</button>
			<button type="primary" plain="ture" @click="pigLiveEdit" style="width: 45%;">修 改</button>
		</view>

		<!-- 修改 -->
		<view class="example" v-if="!showStatus">
		  <uni-forms ref="form" :model="pigLiveForm"   :rules="rules" labelWidth="80px">
		    <uni-forms-item label="猪只耳号" name="pigCode" required>
		      <uni-easyinput v-model="pigLiveForm.pigCode" placeholder="请输入猪只耳号" />
		    </uni-forms-item>
			  <uni-forms-item label="已产胎次" name="pigParity" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pigParity" placeholder="请输入已产胎次（天）" />
			  </uni-forms-item>
			  <uni-forms-item label="栏舍类型" name="phouseType" required>
				  <uni-data-select v-model="pigLiveForm.phouseType" :placeholder="phouseType"
								   :localdata="phouseTypes"
				  ></uni-data-select>
			  </uni-forms-item>
			  <uni-forms-item label="栏舍编码" name="phouseCode" required>
				  <uni-easyinput v-model="pigLiveForm.phouseCode" placeholder="请输入栏舍编码" />
			  </uni-forms-item>
			  <uni-forms-item label="公猪耳号" name="pigFatherCode" required>
				  <uni-easyinput v-model="pigLiveForm.pigFatherCode" placeholder="请输入配种公猪耳号" />
			  </uni-forms-item>
			  <uni-forms-item label="妊娠天数" name="pregnancyDays" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pregnancyDays" placeholder="请输入妊娠天数(天)" />
			  </uni-forms-item>
			  <uni-forms-item label="产程时间" name="pigLiveTime" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pigLiveTime" placeholder="请输入产程时间(h)" />
			  </uni-forms-item>
			  <uni-forms-item label="分娩时间" name="pigLiveDatetime" required>
				  <uni-datetime-picker type="datetime" v-model="pigLiveForm.pigLiveDatetime"/>
			  </uni-forms-item>
			  <uni-forms-item label="健仔数量" name="pigHealthyNum" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pigHealthyNum" placeholder="请输入乳头数（左）(个)" />
			  </uni-forms-item>
			  <uni-forms-item label="弱仔数量" name="pigWeakNum" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pigWeakNum" placeholder="请输入乳头数（右）(个)" />
			  </uni-forms-item>
			  <uni-forms-item label="畸形数量" name="pigMalfmNum" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pigMalfmNum" placeholder="请输入乳头数（左）(个)" />
			  </uni-forms-item>
			  <uni-forms-item label="死胎数量" name="pigDeadNum" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pigDeadNum" placeholder="请输入乳头数（右）(个)" />
			  </uni-forms-item>
			  <uni-forms-item label="母活仔数" name="pigLiveFemaleAliveNum" required>
				  <uni-easyinput type="number" v-model="pigLiveForm.pigLiveFemaleAliveNum" placeholder="请输入乳头数（右）(个)" />
			  </uni-forms-item>
			<uni-forms-item label="分娩方式" name="pigLiveType" required>
			  <uni-data-select v-model="pigLiveForm.pigLiveType" :placeholder="pigLiveType"
			  	:localdata="pigLiveTypes"
			    ></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="分娩管理员" name="pigLiveManager" required>
			  <uni-data-select v-model="pigLiveForm.pigLiveManager" :placeholder="pigLiveForm.hmyStaff.staffName"
			  	:localdata="staffNameList"
			    ></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="pigLiveForm.remark" placeholder="请输入备注" />
				</uni-forms-item>
			  </uni-forms>
			  <view class="edit-button-group">
			  	<button type="primary" @click="back" style="width: 45%;">返 回</button>
			  	<button type="primary" @click="editPidLive('form')" style="width: 45%;">提 交</button>
			  </view>
			</view>
	</view>
</template>

<script>

	import { getPigLive, delPigLive,  updatePigLive,findAllStaffName} from "@/api/hmy/pigLive";

	export default {
		data() {
			return {
				showStatus:true,
				pigLiveId: '',
				pigLiveList: {
					hmyStaff:{
						staffName:''
					}
				},
				pigLiveType: '',
				pigLiveForm:'',
				phouseType:'',
				pigLivedNum:'',

				pigLiveTypes: [{
				  text: ' 顺产',
				  value: "1"
				},
				{
					text: ' 难产',
					value: "2"
				},
				{
				  text: ' 助产',
				  value: "3"
				}],
				phouseTypes:[{
					text: ' 种猪舍',
					value: "1"
				},{
					text: ' 母猪舍',
					value: "2"
				},{
					text: ' 肉猪舍',
					value: "3"
				}
				],

				rules: {
					pigCode: {
						rules: [{required: true, errorMessage: '猪只耳号不能为空'}]
					},
					pigLiveType: {
						rules: [{required: true, errorMessage: '分娩方式不可为空'}]
					},
					pigFatherCode: {
				  		rules: [{required: true, errorMessage: '配种公猪编号不能为空'}]
					},
					pigLiveTime: {
						rules: [{required: true, errorMessage: '分娩时间不能为空'}]
					}
				},
				//所有物料负责人
				staffNameList: [],
			}
		},

		created() {
			findAllStaffName().then(response => {
				var staffList = response.data;
				staffList.forEach(item => {
					this.staffNameList.push({
						'text': item.staffName,
						'value': item.staffId
					})
				})
				//console.log(this.staffNameList);
			})
		},

		onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			this.pigLiveId = option.id;
			this.getPigLiveInfo();
		},
		methods: {
			getPigLiveInfo() {
				getPigLive(this.pigLiveId).then(response => {
					this.pigLiveForm = response.data;
					this.pigLiveList = response.data;
					// 两个字典类型
					if (this.pigLiveList.pigLiveType == 1) {
						this.pigLiveType = '顺产'
					} else if (this.pigLiveList.pigLiveType == 2) {
						this.pigLiveType = '难产'
					} else if (this.pigLiveList.pigLiveType == 3) {
						this.pigLiveType = '助产'
					}

					// 字典类型  栏舍类型
					if (this.pigLiveList.phouseType == 1) {
						this.phouseType = '种猪舍'
					} else if (this.pigLiveList.phouseType == 2) {
						this.phouseType = '母猪舍'
					}else if (this.pigLiveList.phouseType == 3) {
						this.phouseType = '肉猪舍'
					}

					//隐藏加载框
					uni.hideLoading();
					// console.log(response)
				})
			},

			//删除
			pigLiveDelete() {
				console.log("delete", this.pigLiveList.pigLiveId)
				this.$modal.confirm('是否确认删除分娩记录编号为"' +this.pigLiveList.pigLiveId + '"的数据项？').then(() => {
					return delPigLive(this.pigLiveList.pigLiveId);
				}).then(response => {
					this.$modal.msgSuccess("删除成功");
					this.$tab.navigateTo('/pages/hmy/pigLive/index');
				}).catch(()=>{

				});
			},

			//修改
			pigLiveEdit() {
				console.log("edit", this.pigLiveList.pigLiveId)
				this.showStatus = !this.showStatus
			},
			//返回
			back(){
				this.showStatus = !this.showStatus
				this.getPigLive();
			},

			//修改提交
			editPidLive(form) {
			  this.$refs[form].validate().then(res => {
				  console.log(this.pigLiveForm)
			    updatePigLive(this.pigLiveForm).then(response => {
			      this.$modal.msgSuccess("修改成功");
				  setTimeout(()=>{
				  	this.$tab.navigateTo('/pages/hmy/pigLive/index');
				  },1000)

			    })
			  })
			}
		}
	}
</script>

<style>
	page {
		background-color: #ffffff;
	}

	.button-group {
		display: flex;
		justify-content: space-around;
		width:100%;
		position:fixed;
		bottom:5px;

	}

	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	.edit-button-group{
		display: flex;
		justify-content: space-around;
	}
</style>
